﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DispatchAssist.BusinessLogical.DAL;
using CarolLib;
using DispatchAssist.BusinessLogical.Model;

namespace DispatchAssist.BusinessLogical.BLL
{
    public class AlertRuleManager
    {
        #region Members

        private IDataProvider _provider;
        static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
       // private int CacheTime = 3600;
        private readonly static string ALlAlertRuleCacheKey = "Cache_AlertRule";
        private readonly static string ALlAlertActionDefineCacheKey = "AlertActionDefineCacheKey";


        public static readonly AlertRuleManager Instance = new AlertRuleManager();
        #endregion



        public AlertRuleManager()
        {
            _provider = new SqlDataProvider();
        }



        #region Method Body

        public List<AlertRule> GetAllAlertRuleRecords()
        {
            try
            {
                List<AlertRule> list;
                if (!CacheHelper.TryGet<List<AlertRule>>(ALlAlertRuleCacheKey, out list))
                {
                    list = _provider.GetAllAlertRuleRecords();

                    System.Web.Caching.SqlCacheDependency dependency = new System.Web.Caching.SqlCacheDependency
                      (System.Configuration.ConfigurationManager.AppSettings["CurrentDB"], "AlertRule");

                    CacheHelper.Insert(ALlAlertRuleCacheKey, list, dependency);

                    //CacheHelper.Insert(ALlAlertRuleCacheKey, list, CacheTime);
                }
                return list;
            }
            catch (Exception e)
            {
                Logger.Error("AlertRuleManager. GetAllAlertRuleRecords() " + e.Message);
                return null;
            }

        }

        public bool CreateAlertRule(AlertRule alertRule)
        {
            try
            {
                if (_provider.CreateAlertRule(alertRule))
                {
                    CacheHelper.Remove(ALlAlertRuleCacheKey);
                    return true;
                }
                return false;
            }
            catch (Exception e)
            {
                Logger.Error("AlertRuleManager. CreateAlertRule() " + e.Message);
                return false;
            }
        }

        public bool UpdateAlertRule(AlertRule alertRule)
        {
            try
            {
                if (_provider.UpdateAlertRule(alertRule))
                {
                    CacheHelper.Remove(ALlAlertRuleCacheKey);
                    return true;
                }
                return false;
            }
            catch (Exception e)
            {
                Logger.Error("AlertRuleManager. UpdateAlertRule() " + e.Message);
                return false;
            }
        }
        public bool DeleteAlertRule(Guid alertRuleID)
        {

            try
            {
                if (_provider.DeleteAlertRule(alertRuleID))
                {
                    CacheHelper.Remove(ALlAlertRuleCacheKey);
                    return true;
                }
                return false;
            }
            catch (Exception e)
            {
                Logger.Error("AlertRuleManager. DeleteAlertRule() " + e.Message);
                return false;
            }
        }
        public List<AlertActionDefine> GetAllAlertActionDefineRecords()
        {

            try
            {
                List<AlertActionDefine> list;
                if (!CacheHelper.TryGet<List<AlertActionDefine>>(ALlAlertActionDefineCacheKey, out list))
                {
                    list = _provider.GetAllAlertActionDefineRecords();

                    System.Web.Caching.SqlCacheDependency dependency = new System.Web.Caching.SqlCacheDependency
                    (System.Configuration.ConfigurationManager.AppSettings["CurrentDB"], "AlertActionDefine");

                    CacheHelper.Insert(ALlAlertActionDefineCacheKey, list, dependency);

                 //   CacheHelper.Insert(ALlAlertActionDefineCacheKey, list, CacheTime);
                }
                return list;
            }
            catch (Exception e)
            {
                Logger.Error("AlertActionDefineManager. GetAllAlertActionDefineRecords() " + e.Message);
                return null;
            }
        }

        #endregion

    }
}
